﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using UniversityManagementSystem.DAL;
using UniversityManagementSystem.Gateway;

namespace UniversityManagementSystem.Manager
{
    [Serializable]
    public class CourseManager
    {
        CourseGateway gateway = new CourseGateway();
        public string SaveCourse(Course aCourse)
        {
            string saveCourseMessage = "";
            try
            {
                if(IsNullOrBlank(aCourse.CourseTitle) || IsNullOrBlank(aCourse.CourseName) || IsNullOrBlank(aCourse.CourseDescription) || IsNullOrBlank(aCourse.CourseDepartment) || IsNullOrBlank(aCourse.CouresSemester.ToString(CultureInfo.InvariantCulture)))
                {
                    
                     saveCourseMessage = "Please fill up the fields correctly.";
                    
                }
                else
                {
                    if (gateway.CheckCourse(aCourse))
                    {
                        saveCourseMessage = "Course already exists.";
                    }
                    else if(gateway.SaveCourse(aCourse))
                    {
                        saveCourseMessage = "Course has been added successfully.";
                    }
                    else
                    {
                        saveCourseMessage = "Course cannot be added right now,Please try again later.";
                    }
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return saveCourseMessage;
        }
        public bool IsNullOrBlank(String text)
        {
            return text == null || text.Trim().Length == 0;
        }
        public bool isNumber(string courseCredit)
        {
            bool isNum = false;
            double Num;
            isNum = double.TryParse(courseCredit, out Num);
            return isNum;
        }

        public List<Course> GetCourse(string department)
        {
            return gateway.GetCourse(department);
        }

        public string AssignCourse(Teacher aTeacher, Course aCourse)
        {
            string assignCourseMessage = "";
            try
            {
                
                    bool assignCourse = false;
                    string courseTeacher = "";
                    courseTeacher = gateway.CheckAssignOrNot(aCourse);
                    if (courseTeacher != "")
                    {
                        assignCourseMessage = aCourse.CourseTitle + "is already assigned to " + courseTeacher;
                    }
                    else
                    {
                        aTeacher.TeacherRemainingCredit = aTeacher.TeacherRemainingCredit - aCourse.CourseCredit;
                        if (aTeacher.TeacherRemainingCredit < 0)
                        {
                            aTeacher.TeacherCreditOverflow = (-1) * aTeacher.TeacherRemainingCredit;
                            aTeacher.TeacherRemainingCredit = 0;
                        }
                        assignCourse = gateway.AssignCourse(aTeacher, aCourse);
                        if(assignCourse)
                        {
                            assignCourseMessage = "Course has been assigned successfully.";
                        }
                    }
                
                
            }
            catch (Exception e)
            {
                throw e;
            }
            return assignCourseMessage;
        }

        public List<Course> GelAllCourseBySemester(string department, string semester)
        {
            
            return gateway.GelAllCourseBySemester(department, semester);
        }
        public List<Course> GelAllCourseBySemesterView(string department, string semester)
        {
            return gateway.GelAllCourseBySemesterView(department, semester);
        }
        
        public List<Course> ShowUnassignedCourses(string department, string semester)
        {
            return gateway.ShowUnassignedCourses(department, semester);
        }

        public List<Course> ShowUnschedeuledCourses(string department, string semester)
        {
            return gateway.ShowUnschedeuledCourses(department, semester);
        }

        public List<Course> GetAllCourses()
        {
            return gateway.GetAllCourses();
        }

        public List<Course> GetAllCoursesByBepartment(string department)
        {
            return gateway.GetAllCoursesByDepartment(department);
        }

        public List<Course> GetAllEnrolledCourses(string registrationNo)
        {
            return gateway.GetAllEnrolledCourses(registrationNo);
        }

        public bool CheckCourse(string registrationNo, string courseName)
        {
            return gateway.CheckCourseEnrolled(registrationNo, courseName);
        }

        public string SaveResult(string registrationNo, string courseName, string gradePoint)
        {
            string saveMessage = "";
            try
            {
                if (gateway.SaveResult(registrationNo,courseName,gradePoint))
                {
                    saveMessage = "Result has been saved successfully.";
                }
                else
                {
                    saveMessage = "Unable to save result,please try again later.";
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return saveMessage;
        }
    }
}